package com.activenetwork.util.nexuserror;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import com.activenetwork.listener.ImplicitlyWaitListener;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.WaitUtil;
import com.activenetwork.util.WebDriverFactory;

public class NexusErrors {
	private WaitUtil waitUtil = new WaitUtil();
	private By by;
	
	public NexusErrors(String id){
		this.by = By.xpath("//div[@class='nexus-errors' and @id='" + id + "']//div[@class='alert alert-error']");
	}
	
	public boolean isErrorDisplay(){
		waitUtil.wait(500);
		
		WebDriver driver = EnvContext.webDriverTL.get();
		try{
			WebDriverFactory.unwrap((EventFiringWebDriver)driver, new ImplicitlyWaitListener());
			driver.findElement(by);
			return true;
		}catch(NoSuchElementException e){
			return false;
		}finally{
			WebDriverFactory.wrap((EventFiringWebDriver)driver, new ImplicitlyWaitListener());
		}
	}
	
	public String getErrorMsg(){
		waitUtil.wait(500);
		WebDriver driver = EnvContext.webDriverTL.get();
		try{
			WebDriverFactory.unwrap((EventFiringWebDriver)driver, new ImplicitlyWaitListener());
			return driver.findElement(by).findElement(By.xpath("./div")).getText().trim();
		}catch(NoSuchElementException e){
			return "";
		}finally{
			WebDriverFactory.wrap((EventFiringWebDriver)driver, new ImplicitlyWaitListener());
		}
	}
	
	
	
}
